var that;
//级联对象
function  cascadeObject(name1,name3){
	this.name1 = name1;
/*	this.name2 = name2;
*/	this.name3 = name3;
	this.cascadeCooperorg(name1,name3);
	that = this;
}

cascadeObject.prototype={
	
	//合作机构
	cascadeCooperorg:function(name1,name3){
		
		var dataUrl = contextRootPath + '/cimsCascade/queryListCimsCascadeCooperorg';
		
		$.ajax({
			url: dataUrl,
			type: 'get',
			dataType: 'json',
			data:'',
			error: function(result) {
				alert("请查看返回数据是否正确：" + result);
			},
			success: function(results) {
				$("input[name="+name1+"]").after("<select style='width:173px;height:30px;' id='"+name1+"' name='"+name1+"'></select>");
				$("input[name="+name1+"]").remove();
			/*	$("input[name="+name2+"]").after("<select style='width:173px;' id='agreementCode' name='agreementCode'></select>");
				$("input[name="+name2+"]").remove();*/
				$("input[name="+name3+"]").after("<select style='width:173px;height:30px;' id='"+name3+"' name='"+name3+"'></select>");
				$("input[name="+name3+"]").remove();
				$("select[name="+name1+"]").change(function(){
					var value = $("select[name="+name1+"]").val();
					var dealTypeNO = $("#dealTypes").length;
					var dealType = $("#dealTypes").val();
					if(dealTypeNO<=0){
						dealType='';
					}
					that.cascadeContract(value,name3,dealType);
					
				});
				
//				$("select[name="+name3+"]").change(function(){
//					var value = $("select[name="+name3+"]").val();
//					//returnContract(value);
//					isShowContract();
//				});
				
				var result=results.data;
				var j;
				var resultLength = result.length - 1;
				$("select[name="+name1+"]").html("<option value=''>--请选择--</option>");
				$("select[name="+name3+"]").html("<option value=''>--请选择--</option>");
				for (j = 0; j <= resultLength; j++) {
					$("select[name="+name1+"]").append("<option value='"+result[j].cooperorgCode+"'>"+result[j].cooperorgName+"</option>");
					/*if(null != cooperorgCode && "" !=cooperorgCode){
						
						if(cooperorgCode == result[j].cooperorgCode){
							$("select[name=cooperorgCode]").find("option[value='" + result[j].cooperorgCode + "']").attr("selected",true);
							
							cascadeContract($('#cooperorg'),contractCode);
						}
					}*/
				}
				
				
			}
			
			
		});
	},
	
	//协议
	cascadeAgreement:function(value,name2,name3,name1,obj){
		$("select[name="+name2+"] option").remove();
		
		var dataUrl = contextRootPath + '/cimsCascade/queryListCimsCascadeContracts?cooperorgCode=' + value;
		
		$.ajax({
			url: dataUrl,
			type: 'get',
			dataType: 'json',
			error: function(result) {
				alert("请查看返回数据是否正确：" + result);
			},
			success: function(results) {
				var result=results.data;
				$("select[name="+name2+"]").html("<option value=''>--请选择--</option>");
				var j;
				var resultLength = result.length - 1;
				for (j = 0; j <= resultLength; j++) {
					$("select[name="+name2+"]").append("<option value='"+result[j].agreementCode+"'>"+result[j].agreementName+"</option>");
					/*if(null != contractCode && "" !=contractCode){
						
						if(contractCode == result[j].contractCode){
							$('#cooperorg').find("option[value='" + result[j].cooperorgCode + "']").attr("selected",true);
							
						}
					}*/
				}
				
				var agreementLength =$("select[name="+name2+"] option").length;
				if(agreementLength>1){
					var value1 = $("select[name="+name1+"]").val();
					var value2 = $("select[name="+name2+"]").val();
					obj.cascadeContract(value1,value2,name3);
				}
			}
			
		});
	},

	
	//合约
	cascadeContract:function(value1,name3,dealType){
		$("select[name="+name3+"] option").remove();
		
		var dataUrl = contextRootPath + '/cimsCascade/queryListCimsCascadeContracts?cooperorgCode=' + value1 + '&dealType=' + dealType;
		
		$.ajax({
			url: dataUrl,
			type: 'get',
			dataType: 'json',
			error: function(result) {
				alert("请查看返回数据是否正确：" + result);
			},
			success: function(results) {
				var result=results.data;
				$("select[name="+name3+"]").html("<option value=''>--请选择--</option>");
				var j;
				var resultLength = result.length - 1;
				for (j = 0; j <= resultLength; j++) {
					$("select[name="+name3+"]").append("<option value='"+result[j].contractCode+"'>"+result[j].contractName+"</option>");
					/*if(null != contractCode && "" !=contractCode){
						
						if(contractCode == result[j].contractCode){
							$('#cooperorg').find("option[value='" + result[j].cooperorgCode + "']").attr("selected",true);
							
						}
					}*/
				}
			}
			
		});
	}
};

(function( $ ){  
	  $.newCascadeObject = function(name1,name3) {
	   	return new cascadeObject(name1,name3);
	  };  
})(jQuery);

